package com.web.controller;

import com.alibaba.fastjson2.JSON;
import com.web.entity.ResponseResult;
import com.web.entity.Student;
import com.web.service.StudentUpdateService;
import com.web.service.impl.StudentUpdateServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lenovo
 * @Description
 * @Date2025-3-1 23:04
 */
@WebServlet("/upadteStudent")
public class StudentUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //设置相应类型
        resp.setContentType("application/json;charset=UTF-8");

        try {
            int id = Integer.parseInt(req.getParameter("id"));
            String name = req.getParameter("name");
            int age = Integer.parseInt(req.getParameter("age"));
            String gender = req.getParameter("gender");
            String address = req.getParameter("address");
            String cellphone = req.getParameter("cellphone");

            Student student = new Student();
            student.setId(id);
            student.setName(name);
            student.setAge(age);
            student.setGender(gender);
            student.setAddress(address);
            student.setCellphone(cellphone);

            StudentUpdateService studentUpdateService = new StudentUpdateServiceImpl();
            int result = studentUpdateService.updateStudentById(student);

            if (result > 0) {
                resp.getWriter().write(JSON.toJSONString(new ResponseResult(200, "学生更新成功", student)));
            } else {
                resp.getWriter().write(JSON.toJSONString(new ResponseResult(500, "学生更新失败", null)));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        }
}
